#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1e6+10;

int val[N<<2],rt[N],ls[N<<2],rs[N<<2],ans[N],Q,n,cnt,stk[N],h[N],top,mx,siz[N],ccnt,fh[N],ffqc,mems[N<<2],memt;
struct fq{int id,nxt;}ffq[N<<1];
struct edge{int v,nxt;}e[N];
struct QUERY{
	int x,k;
}q[N];
void link(int x,int y){e[++ccnt] = (edge){y,h[x]};h[x] = ccnt;}
void adq(int x,int y){ffq[++ffqc] = (fq){y,fh[x]};fh[x] = ffqc;}

void dfs(int x,int dep = 1){siz[x] = 1;mx = max(mx,dep);for(int i = h[x],y;i;i = e[i].nxt) dfs(y = e[i].v,dep+1),siz[x] += siz[y];}

int newnode(){
	if(memt) return mems[memt--];
	return ++cnt;
}

void merge(int &y,int x,int l = 1,int r = mx){
	if(!y || !x) return y = x + y,void(); int mid = l + r >> 1;
	if(l == r) {val[y] += val[x]; ls[x] = rs[x] = val[x] = 0; mems[++memt] = x;return;}
	val[y] = val[y] + val[x]; merge(ls[y],ls[x],l,mid); merge(rs[y],rs[x],mid+1,r);
	rs[x] = ls[x] = val[x] = 0; mems[++memt] = x;
}

void upd(int &y,int pos,int l = 1,int r = mx){
	if(!y) y = newnode(); val[y]++; if(l == r) return; int mid = l + r >> 1;
	if(pos <= mid) upd(ls[y],pos,l,mid); else upd(rs[y],pos,mid+1,r);
}

int query(int y,int pos,int l = 1,int r = mx){
	if(!y) return 0; if(l == r) return val[y]; int mid = l + r >> 1;
	if(pos <= mid) return query(ls[y],pos,l,mid); else return query(rs[y],pos,mid+1,r);
}

void dfs2(int x){
	stk[++top] = x;
	for(int i = fh[x];i;i = ffq[i].nxt){
		if(top <= q[ffq[i].id].k) continue;
		int y = stk[top - q[ffq[i].id].k]; adq(y,ffq[i].id);
	}fh[x] = 0;
	for(int i = h[x],y;i;i = e[i].nxt) dfs2(y = e[i].v);
	top--;
}

bool cmp(int x,int y){return siz[x] > siz[y];}

void dfs1(int x,int dep = 1){
	int lst = top; for(int i = h[x];i;i = e[i].nxt) stk[++top] = e[i].v; sort(stk + lst + 1,stk + top + 1);
	for(int i = lst + 1;i <= top;i++) dfs1(stk[i],dep+1),merge(rt[x],rt[stk[i]]); top = lst;
	upd(rt[x],dep);
	for(int i = fh[x];i;i = ffq[i].nxt)
		ans[ffq[i].id] = query(rt[x],dep + q[ffq[i].id].k) - 1;
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in;Q = in; for(int i = 2;i <= n;i++) link(in,i);
	dfs(1);
	for(int i = 1;i <= Q;i++){
		q[i].x = in,q[i].k = in;
		adq(q[i].x,i);
	}dfs2(1);top = 0; dfs1(1);
	for(int i = 1;i <= Q;i++) printf("%d%c",ans[i]," \n"[i == Q]);
	return 0;
}
